package com.fashion.service;

import com.fashion.dao.NewsSourceDao;
import com.fashion.model.NewsSource;
import com.fashion.utils.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: gnyk
 * Date: 13-2-23
 */
@Service("newsSourceService")
public class NewsSourceServiceImpl implements NewsSourceService {
    private NewsSourceDao newsSourceDao;

    @Override
    public void saveOrUpdate(Object object) {
        newsSourceDao.saveOrUpdate(object);
    }

    @Override
    public void deleteEntityById(String id) {
        newsSourceDao.deleteById(id, NewsSource.class);
    }

    @Override
    public void deleteEntity(Object object) {
        newsSourceDao.delete(object);
    }

    @Override
    public List<NewsSource> findAllNewsSources() {
        return newsSourceDao.find("from NewsSource");
    }

    @Override
    public List<NewsSource> searchNewsSource(String newsSourceTitle, int page, int rows) {
        StringBuilder hql = new StringBuilder("from NewsSource ns ");
        Map<String, Object> parameters = new HashMap<String, Object>();

        if (!StringUtils.isNullOrEmpty(newsSourceTitle)) {
            hql.append(" where ns.title like :title");
            parameters.put("title", newsSourceTitle + "%");
        }
        hql.append(" order by ns.lastUpdate");

        return newsSourceDao.findItemsWithPage(hql.toString(), parameters, page, rows);
    }

    @Override
    public int countNewsSource(String newsSourceTitle) {
        StringBuilder hql = new StringBuilder("select count(ns.id) from NewsSource ns ");
        Map<String, Object> parameters = new HashMap<String, Object>();
        if (!StringUtils.isNullOrEmpty(newsSourceTitle)) {
            hql.append(" where ns.title like :title");
            parameters.put("title", newsSourceTitle + "%");
        }

        return newsSourceDao.countItems(hql.toString(), parameters);
    }

    @Override
    public NewsSource getNewsSourceById(String id) {
        return newsSourceDao.getEntityById(NewsSource.class, id);
    }

    @Override
    public String testNewsSource(NewsSource newsSource) {
        StringBuilder builder = new StringBuilder();
        try {
            Document document = Jsoup.parse(new URL(newsSource.getStartUrl()), 30 * 1000);
            builder.append("startUrl: OK\n");

            Elements newsLinks = document.select(newsSource.getListSelector());

            if (newsLinks.size() > 0) {
                builder.append("listSelector: OK\n");

                Element newsLink = newsLinks.get(0);
                String newsHref = newsLink.attr("href");
                testNewsLink(new URL(newsSource.getListBasePath() + newsHref), newsSource, builder);
            }

            Elements nextList = document.select(newsSource.getListNext());
            if (nextList.size() > 0) {
                builder.append("listNext: OK\n");
            }else{
                builder.append("listNext: Fail\n");
            }

        } catch (IOException e) {
            builder.append("startUrl: Fail\n");
            return builder.toString();
        }

        return builder.toString();
    }

    protected void testNewsLink(URL url, NewsSource newsSource, StringBuilder builder) {
        try {
            Document document = Jsoup.parse(url, 60 * 1000);
            builder.append("newsLink: OK\n");

            Elements dateElements = document.select(newsSource.getDate());
            if (dateElements.size() > 0) {
                builder.append("date: OK\n");
            } else {
                builder.append("date: Fail\n");
            }


            Elements titleElements = document.select(newsSource.getTitle());
            if (titleElements.size() > 0) {
                builder.append("title: OK\n");
            } else {
                builder.append("title: Fail\n");
            }

            String introductionSelector = newsSource.getIntroduction();
            if (!StringUtils.isNullOrEmpty(introductionSelector)) {
                Elements introductionElements = document.select(introductionSelector);

                if (introductionElements.size() > 0) {
                    builder.append("introduction: OK\n");
                } else {
                    builder.append("introduction: Fail\n");
                }
            }

            Elements bodyElements = document.select(newsSource.getContext());

            if (bodyElements.size() > 0) {
                builder.append("body: OK\n");
            } else {
                builder.append("body: Fail\n");
            }

            Elements nextElements = document.select(newsSource.getNext());
            if (nextElements.size() > 0) {
                builder.append("newsNext: OK\n");
            }


        } catch (IOException e) {
            builder.append("newsLink: fail\n");
        }
    }

    @Resource(name = "newsSourceDao")
    public void setNewsSourceDao(NewsSourceDao newsSourceDao) {
        this.newsSourceDao = newsSourceDao;
    }
}
